package util.xml;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;

import com.thoughtworks.xstream.XStream;

public class XMLHelper{
	
	public static String toXML(Serializable vo){
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		
		return xstream.toXML(vo);
	}
	public static Object fromXML(String xml){
		/*Los tags tienen que tener el nombre del paquete*/
		Object ret = null;
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);
		
		return xstream.fromXML(xml);
	}
	@SuppressWarnings("rawtypes")
	public static Object fromXML(String xml, Map<String, Class> aliassesMap){
		/*Trabaja con alias para cuando el tag xml no incluye
		 * los paquetes, asi puede asociar un tag con una clase*/
		Object ret = null;
		XStream xstream = new XStream();
		xstream.autodetectAnnotations(true);		
		
		for(Entry<String, Class> entry : aliassesMap.entrySet()){
			xstream.alias(entry.getKey(), entry.getValue());
		}
		
		return xstream.fromXML(xml);
	}
}
